<?php
namespace app\api\controller\v1;

use app\common\controller\Api;
use app\common\model\Classes;
use app\common\model\ClassStudent;

class My extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function getInfo()
    {
        $user = $this->auth->getUser();

        $userInfo =\app\admin\model\User::alias('u')
            ->field('u.*, c.name as class_name, c.id as class_id')
            ->join('eb_class_student cs', 'u.id = cs.student_id', 'LEFT')
            ->join('eb_classes c', 'cs.classes_id = c.id', 'LEFT')
            ->where('u.id', $user['uid'])
            ->find();

        if (!$userInfo) {
            $this->error('用户不存在');
        }

        // 获取学分信息
        $credit = \app\admin\model\Credits::where('user_id', $user['uid'])->sum('value');
        $userInfo['credit'] = $credit ?: 0;

        // 获取已报班级数量
        $classCount =  \app\admin\model\Student::alias('s')
            ->where('student_id', $user['uid'])
            ->count();
        $userInfo['class_count'] = $classCount;

        $this->success('获取成功', $userInfo);
    }
}
